<template>
  <el-drawer
    :close-on-click-modal="false"
    :show-close="false"
    class="drawer"
    style="border-radius: 24px; height: 430px; margin-top: 50px"
  >
    <template #header>
      <h3 class="unselectable">添加角色</h3>
      <el-icon
        style="color: #000; font-size: 25px; filter: drop-shadow(0 0 3px #000)"
        @click="cancel($parent)"
      >
        <Close />
      </el-icon>
    </template>
    <template #default>
      <el-form>
        <el-form-item label="职位名称">
          <el-input
            placeholder="请输入职位名称"
            v-model="roleData.roleName"
          ></el-input>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <el-button type="primary" @click="cancel($parent)">取消</el-button>
      <el-button
        type="primary"
        @click="confirm($parent)"
        :disabled="roleData.roleName ? false : true"
      >
        确定
      </el-button>
    </template>
  </el-drawer>
</template>

<script lang="ts">
export default {
  name: 'AddCard',
}
</script>

<script setup lang="ts">
import { watch, reactive } from 'vue'
import { reqAddOrModify } from '../../../../api/acl/part'
import { ElMessage } from 'element-plus'
import type { Record } from '../../../../api/acl/part/getRoleListType'
const props = defineProps({
  currentData: Object,
})

let roleData = reactive<Record>({
  roleName: '',
})
//监测传递过来的角色信息变化，即当用户点击不同角色进行编辑时候，可以传递与之对应的角色信息
watch(
  () => {
    props.currentData
  },
  () => {
    Object.assign(roleData, props.currentData)
  },
  { deep: true, immediate: true },
)
//抽屉关闭回调
function cancel(parent: any) {
  parent.getRoleList()
  roleData.roleName = ''
  parent.drawer = false
}
//确定回调
async function confirm(parent: any) {
  if (roleData.id) {
    let result: any = await reqAddOrModify(roleData)
    ElMessage({
      type: 'success',
      message: '修改成功',
    })
  } else {
    //请求接口
    let result: any = await reqAddOrModify(roleData)
    /*  console.log(result) */
    ElMessage({
      type: 'success',
      message: '添加成功',
    })
  }
  parent.getRoleList()
  roleData.roleName = ''
  parent.drawer = false
  roleData.id = 0
}
</script>

<style scoped lang="scss">
.drawer {
  border-radius: 20px;
}
</style>
